package com.one.group.user.lister;

import com.one.group.user.service.HouseCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class HouseCacheListener {

    @Autowired
    private HouseCacheService houseCacheService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "house.cache"),
            exchange = @Exchange(name = "gxa.direct"), key = {"buildingUpdate"}))
    public void listenerBuildingUpdateMessage(Long buildingtypeId) {
        log.info("监听到删除楼栋缓存消息,删除缓存:{}",buildingtypeId);
        //删除小区缓存
        houseCacheService.deleteBuildingCache(buildingtypeId);
    }
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "house.cache"),
            exchange = @Exchange(name = "gxa.direct"), key = {"houseUpdate"}))
    public void listenerHouseDeleteMessage(Long unitId) {
        log.info("监听到删除房屋缓存消息,删除缓存:{}",unitId);
        houseCacheService.deleteHouseCache(unitId);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "house.cache"),
            exchange = @Exchange(name = "gxa.direct"), key = {"allHouseUpdate"}))
    public void listenerAllHouseDeleteMessage(Long unitId) {
        log.info("监听到删除房屋缓存消息,删除缓存:{}",unitId);
        houseCacheService.deleteHouseCache(unitId);
    }
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "house.cacheAAA"),
            exchange = @Exchange(name = "gxa.direct"), key = {"deleteAAAA"}))
    public void listenerAllHouseDeleteMessage(String  message) {
        log.info("监听到删除所有缓存消息,删除缓存:{}");
        if(redisTemplate.delete(message)){
            log.info("删除缓存成功");
        }else log.info("删除缓存失败");
    }
}

